<template>
	<view class="textComparison_box">
		<view class="content">
			<view class="textarea">
				<u-textarea height="362rpx" maxlength="2000" v-model="oldText" border="false"
					placeholder="原文本（点击输入文案或长按粘贴文案）">
				</u-textarea>
			</view>
			<view class="tools">
				<view class="tools_left">
					<view class="left" @click="delOldText">
						<u-icon name="trash" size="18" color="#999999"></u-icon>
						<text class="text">清除文本</text>
					</view>
				</view>
				<view class="tools_right">
					<text class="text">字数 {{oldText.length}}</text>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="textarea">
				<u-textarea height="362rpx" maxlength="2000" v-model="newText" border="false"
					placeholder="要比较的文本（点击输入文案或长按粘贴文案">
				</u-textarea>
			</view>
			<view class="tools">
				<view class="tools_left">
					<view class="left" @click="delNewText">
						<u-icon name="trash" size="18" color="#999999"></u-icon>
						<text class="text">清除文本</text>
					</view>
				</view>
				<view class="tools_right">
					<text class="text">字数 {{newText.length}}</text>
				</view>
			</view>
		</view>
		<view class="button">
			<view class="button_left" @click="delAllText">
				<u-button text="重新输入" color="#5490FF" size="large"></u-button>
			</view>
			<view class="button_right" @click="textComparison">
				<u-button text="文本比较" color="#1EB930" size="large"></u-button>
			</view>
		</view>
		<view class="results_box" v-if="isShowResults">
			<view class="results_top">
				<text class="results_top_center">结果比较</text>
				<text class="results_top_right">相似度：{{similarity}}%</text>
			</view>
			<view class="results_text">
				<text user-select='true' v-for="(item,index) in  resultsText" :key='index'
					:style="{color:!!item.color?item.color:'#606266',fontSize:!!item.fontSize?item.fontSize:'15px'}">{{item.value}}</text>
			</view>
		</view>
		<view style="background-color: f1f1f1; height: 40rpx;"></view>
		<!-- 非会员看广告提示 -->
		<u-modal :show="vipShow" :title="'温馨提示'" :content="content" cancelText="取消" confirmText="去开通"
			showCancelButton="true" @cancel="cancel" @confirm="govip" :closeOnClickOverlay="true"
			@close="vipShow = false"></u-modal>
	</view>
</template>

<script>
	import {
		wxTextDetection
	} from "../../utils/textCheck.js";
	import {
		isLogin,
		vipType
	} from '@/business/login.js';
	let app = getApp();
	var diff = require('diff');
	export default {
		data() {
			return {
				vipShow: false,
				content: '您还不是会员,此功能需开通会员才可使用。',
				oldText: '',
				newText: '',
				resultsText: '',
				similarity: 0,
				isShowResults: false
			};
		},
		onShow() {
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			// this.isVip = true;
		},
		methods: {
			textComparison() {
				if (this.oldText.trim() == '') {
					uni.showToast({
						title: '请输入原文本',
						duration: 2000,
						icon: 'none'
					});
				} else if (this.newText.trim() == '') {
					uni.showToast({
						title: '请输入要比较文本',
						duration: 2000,
						icon: 'none'
					});
				} else if (this.isVip) {
					wxTextDetection((this.newText + this.oldText)).then((res) => {
						if (res.isItPass) {
							let diffResult = diff.diffChars(this.oldText, this.newText)
							console.log(diffResult)
							if (diffResult.length) {
								diffResult.map(item => {
									if (!!item.added) {
										item.color = '#73A1FF'
									}
									if (!!item.removed) {
										item.color = '#999999'
										item.fontSize = '12rpx'
									}
								})
							}
							this.resultsText = diffResult
							this.similarity = this.similar(this.oldText, this.newText, 2)
							this.isShowResults = true
						} else {
							// wx.showModal({
							//   title: "提示",
							//   content: "检测到敏感关键词，请修改后重试！",
							//   showCancel: false,
							//   success(res) {},
							// });
						}
					})


				} else {
					this.vipShow = true
				}

			},
			similar(s, t, f) {
				if (!s || !t) {
					return 0
				}
				if (s === t) {
					return 100;
				}
				var l = s.length > t.length ? s.length : t.length
				var n = s.length
				var m = t.length
				var d = []
				f = f || 2
				var min = function(a, b, c) {
					return a < b ? (a < c ? a : c) : (b < c ? b : c)
				}
				var i, j, si, tj, cost
				if (n === 0) return m
				if (m === 0) return n
				for (i = 0; i <= n; i++) {
					d[i] = []
					d[i][0] = i
				}
				for (j = 0; j <= m; j++) {
					d[0][j] = j
				}
				for (i = 1; i <= n; i++) {
					si = s.charAt(i - 1)
					for (j = 1; j <= m; j++) {
						tj = t.charAt(j - 1)
						if (si === tj) {
							cost = 0
						} else {
							cost = 1
						}
						d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
					}
				}
				let res = (1 - d[n][m] / l) * 100
				return res.toFixed(f)
			},
			delAllText() {
				this.oldText = ''
				this.newText = ''
				this.isShowResults = false
			},
			delOldText() {
				this.oldText = ''
			},
			delNewText() {
				this.newText = ''
			},
			cancel() {
				this.vipShow = false
			},
			govip() {
				this.vipShow = false;
				if (uni.getStorageSync('OS') !== 'ios') {
					uni.navigateTo({
						url: '/pages/paymentCenter/paymentCenter'
					});
				} else {
					if (getApp().globalData.iosPay == 0) {
						uni.showModal({
							title: '提示',
							content: '由于相关规范，IOS功能暂不可用',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else if (getApp().globalData.iosPay == 1) {
						uni.navigateTo({
							url: '/pages/goods/goods'
						});
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	/deep/.u-textarea--disabled {
		background-color: #fff !important;
	}

	/deep/.u-textarea {
		padding-top: 0 !important;
	}

	/deep/.u-button {
		border-radius: 12rpx !important;
	}

	/deep/.u-icon__icon {
		margin-right: 8rpx !important;
	}

	.results_box {
		margin-top: 39rpx;
		width: 696rpx;
		// height: 274rpx;
		background: #FFFFFF;
		border-radius: 18rpx;

		.results_top {
			width: 100%;
			height: 111rpx;

			.results_top_center {
				margin-left: 268rpx;
				font-size: 34rpx;
				line-height: 111rpx;
				font-family: PingFang SC;
				color: #5490FF;
			}

			.results_top_right {
				margin-left: 97rpx;
				font-size: 25rpx;
				font-family: PingFang SC;
				color: #333333;
				line-height: 36px;
			}
		}

		.results_text {
			padding: 0 20rpx 20rpx 20rpx;
		}
	}

	.button {
		margin-top: 62rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;

		.button_left {
			width: 47%;
		}

		.button_right {
			width: 47%;
		}
	}

	.textarea {
		height: 362rpx;
	}

	.text {
		font-size: 19rpx;
		font-family: PingFang SC;
		color: #999999;
	}

	.textComparison_box {
		padding: 0 27rpx;

		.content {
			margin-bottom: 19rpx;
			width: 696rpx;
			height: 462rpx;
			border-radius: 18rpx;
			background-color: #fff;
		}

		.tools {
			height: 100rpx;
			padding: 0 27rpx;
			display: flex;
			justify-content: space-between;

			.tools_left {
				display: flex;
				justify-content: space-between;

				.left {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}

				.right {
					display: flex;
					align-items: center;
					margin-top: 8rpx;
				}
			}

			.tools_right {
				display: flex;
				align-items: center;
			}
		}

	}
</style>
<style>
	page {
		background-color: #f4f4f4;
	}
</style>